/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aaron.platform.view;

import com.aaron.platform.dao.PageInfo;
import com.aaron.platform.entity.BaseObject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.apache.myfaces.custom.datascroller.ScrollerActionEvent;

/**
 * 数据列表抽象类
 * @author Aaron
 * @version 1.0
 */
public abstract class DataListBean {

    /** 数据列表 */
    protected DataModel data;

    /** 排序列名 */
    protected String sortColumn;

    /** 排序方式 */
    protected boolean ascending;

    /** 分页信息 */
    protected PageInfo pageInfo;

    /** 需要显示的列 */
    protected List<ColumnType> headerColumns;

    protected int dataColumnIndex;

    /** 是否排序 */
    protected boolean sortable;

    /** 是否允许多选 */
    protected boolean multiSelectable;

    /** 是否已全选 */
    protected boolean allSelected;


    /** 是否分页 */
    protected boolean pagable;

    /** 是否可编辑 */
    protected boolean editable;

    /** 是否可以查看明细 */
    protected boolean viewable;

    /** 是否可删除 */
    protected boolean deletable;

    /** 列表名 */
    protected String tableTitle;

    /**
     * 构造方法
     */
    public DataListBean()
    {
        /* 设置默认值 */
        headerColumns = new ArrayList<ColumnType>();
        dataColumnIndex = 0;
        sortColumn = "";

        /* 通用属性 */
        sortable = false;
        multiSelectable = false;
        allSelected = false;
        pagable = false;

        /* 通用操作列 */
        editable = false;
        viewable = false;
        deletable = false;
        
        /* 初始化表头 */
        initColumnHeaders();
    }

    /**
     * 查询数据列表
     */
    public abstract void queryData();

    /**
     * 初始化表头
     */
    public abstract void initColumnHeaders();

    /**
     *
     * @param columnName
     * @return 显示的值
     */
    public abstract String getColumnValue();

    public String getColumnName()
    {
        ColumnType colType = headerColumns.get(dataColumnIndex);
        return colType.getColumnName();
    }

    public void increaseColumnIndex()
    {
        dataColumnIndex++;
         if (dataColumnIndex == headerColumns.size())
        {
            dataColumnIndex = 0;
        }
    }

    public boolean isSelected()
    {
        BaseObject object = (BaseObject) data.getRowData();
        return object.isSelected();
    }

    public void setSelected(boolean selected)
    {
        BaseObject object = (BaseObject) data.getRowData();
        object.setSelected(selected);
    }

    public void selectOrCancelAll(ValueChangeEvent event)
    {
        List<BaseObject> list = (List<BaseObject>) data.getWrappedData();
        for (Iterator<BaseObject> it = list.iterator(); it.hasNext();) {
            BaseObject object = it.next();
            object.setSelected(allSelected);
        }
    }

    public void selectOrCancelSingle(ValueChangeEvent event)
    {
        List<BaseObject> list = (List<BaseObject>) data.getWrappedData();
        for (Iterator<BaseObject> it = list.iterator(); it.hasNext();) {
            BaseObject object = it.next();
            if (allSelected & object.isSelected() == false){
                allSelected = false;
                break;
            }
        }
    }

    /**
     * 编辑数据
     * @return 返回页面导航
     */
    public String editData()
    {
        return null;
    }

    /**
     * 查看明细
     * @return 返回页面导航
     */
    public String showDetail()
    {
        return null;
    }

    /**
     * 删除数据
     * @return 返回页面导航
     */
    public String deleteData()
    {
        return null;
    }

    /**
     * 刷新数据
     */
    public void refreshData()
    {
        if (pageInfo != null) {
            pageInfo.init();
        }
        queryData();
    }

    /**
     * 翻页操作，使用on-demand方式
     * @param action 翻页操作
     * @return
     */
    public String scrollAction(ActionEvent action) {
        ScrollerActionEvent scrollerEvent = (ScrollerActionEvent) action;
        String facet = scrollerEvent.getScrollerfacet();
        if (facet.equals("next")) {
            this.pageInfo.getNextIndex();
            queryData();
        } else if (facet.equals("previous")) {
            this.pageInfo.getPreviousIndex();
            queryData();
        } else if (facet.equals("first")) {
            this.pageInfo.getfirstIndex();
            queryData();
        } else if (facet.equals("last")) {
            this.pageInfo.getLastIndex();
            queryData();
        }
        return null;
    }

    public DataModel getData() {
        if (data == null) {
            data = new ListDataModel();
        }
        //queryData();
        return data;
    }

    public String getSortColumn() {
        return sortColumn;
    }

    public List<ColumnType> getHeaderColumns() {
        return headerColumns;
    }

    public boolean isDeletable() {
        return deletable;
    }

    public boolean isEditable() {
        return editable;
    }

    public boolean isMultiSelectable() {
        return multiSelectable;
    }

    public boolean isPagable() {
        return pagable;
    }

    public boolean isSortable() {
        return sortable;
    }

    public boolean isViewable() {
        return viewable;
    }

    public String getTableTitle() {
        return tableTitle;
    }

    public int getDataColumnIndex() {
        return dataColumnIndex;
    }

    public boolean isAllSelected() {
        return allSelected;
    }

    public void setAllSelected(boolean allSelected) {
        this.allSelected = allSelected;
    }

    public PageInfo getPageInfo() {
        return pageInfo;
    }

}
